package com.it.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.it.entity.Hobby;
import com.it.entity.User;
import com.it.entity.UserHobby;
import com.it.mapper.HobbyMapper;
import com.it.mapper.UserHobbyMappper;
import com.it.service.HobbyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HobbyServiceImpl implements HobbyService {
    @Resource
    private HobbyMapper hobbyMapper;
    @Resource
    private UserHobbyMappper userHobbyMappper;

    @Override
    public int selectCountForUserHobbies(User user) {
        String id = user.getId();
        List<Hobby> hobbies = hobbyMapper.selectHobbies(id);
        return hobbies.size();
    }

    @Override
    public boolean insertHobby(Hobby hobby) {
        Integer insert = hobbyMapper.insert(hobby);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertHobbyToAllHobbies(User user, Hobby hobby) {
        int i = hobbyMapper.insertHobbyToAllHobbies(hobby.getId(), user.getId());
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteMyHobbies(Integer id, String userId) {
        EntityWrapper<UserHobby> userHobbyEntityWrapper = new EntityWrapper<>();
        userHobbyEntityWrapper.eq("id", id);
        userHobbyEntityWrapper.eq("userId", userId);
        Integer deleteStatus = userHobbyMappper.delete(userHobbyEntityWrapper);
        if (deleteStatus > 0) {
            return true;
        }
        return false;
    }
}
